<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018-05-22
 * Time: 15:38
 */

namespace app\api\validate;


use app\lib\exception\ParameterException;

class CheckRefund extends BaseValidate
{
    protected $rule = [
        'refund_oid' => 'require|isPositiveInteger',
        'refund_type' => 'require|isPositiveInteger',
        'refund_reason' => 'require|isNotEmpty',
        'refund_money' => 'require|isNotEmpty',
        'refund_imgs' => 'isFileTypeImage',
        'refund_logistics_name' => 'isOriginValue',
        'refund_logistics_no' => 'isOriginValue',
    ];
    //    验证文件类型是否图片格式
    protected function isFileTypeImage($value){
        $type_array = array('jpg','jpeg','png','gif');//目标文件类型
        if($value){
            if(is_array($value)){
                foreach ($value as $k => $v){
                    $result = self::checkFileTypeImage($v, $type_array);
                    if($result){
                        return true;
                    }
                }
            }else{
                $result = self::checkFileTypeImage($value, $type_array);
                if($result){
                    return true;
                }
            }
        }else{
            return true;
        }
    }
    public function checkFileTypeImage($v, $type_array){
        $need_type = strtolower(implode('/',$type_array));
        $res = BaseValidate::isFileType($v,$type_array);
        if($res){
            return true;
        }else{
            throw new ParameterException([
                'msg' => '文件类型错误，必须为'.$need_type.'类型'
            ]);
        }
    }

}